/**
 * Created by Esri on 2016/6/28.
 */
define([ "dojo/_base/declare",
        "gistech/widgets/baseWidget/BaseWidget",
        "gistech/widgets/ToolBox/MeasureLength",
        "gistech/widgets/ToolBox/MeasureArea",
        "gistech/widgets/baseMap/BaseMap",
        "dojo/_base/lang",
        "dojo/on",
        "dojo/_base/connect",
        "dojo/dom-construct",
        "dojo/query",
        "dojo/dom-style",
        "dojo/dom-class",
        "dojo/dom-attr",
        "dojo/topic",
        "esri/layers/FeatureLayer",
        "esri/symbols/PictureMarkerSymbol",
        "esri/layers/ArcGISTiledMapServiceLayer",
        "esri/layers/ArcGISDynamicMapServiceLayer",
        "../../util/ajaxUtil",
        "dojo/text!./templates/Template.html"],
    function(declare,BaseWidget,MeasureLength,MeasureArea,BaseMap,lang,on,connect,domConstruct,query, 
    		domStyle,domClass,domAttr,topic,FeatureLayer,PictureMarkerSymbol,ArcGISTiledMapServiceLayer,ArcGISDynamicMapServiceLayer,ajaxUtil,template) {
        return declare([BaseWidget], {
            templateString:template,
            map:null,
            constructor:function(args) {
            	debugger
                this.inherited(arguments);
                this.map=args.map;
                this.showBorder = true;
                this.container = args.container;
                this.baseMap=null;
            },
            postMixInProperties:function () {
                this.inherited(arguments);
            },
            postCreate : function() {
            	debugger
            	this.init();
                this.bindEvent();
                this.inherited(arguments);
            },
            startup:function(){
                this.inherited(arguments);
            },
            destroy:function(){
                this.inherited(arguments);
            },
            init:function(){	
            	domConstruct.place(this.domNode, this.container);
            },
            bindEvent:function(){
            	connect.connect(this.basemapBtn, "click", lang.hitch(this, this._basemapBtnClick));
            	connect.connect(this.meatureDistanceBtn, "click", lang.hitch(this, this._meatureDistanceBtnClick));
            	connect.connect(this.clearBtn, "click", lang.hitch(this, this._clearBtn));
            	connect.connect(this.meatureAreaBtn, "click", lang.hitch(this, this.meatureAreaBtnClick));
            	connect.connect(this.homeBtn, "click", lang.hitch(this, this._homeBtnClick));
            },
            /**实现菜单的展开与折叠*/
            toggleToolBox:function(isExpand){
            	debugger
            	if(isExpand){
            		domClass.remove(this.domNode, "showtoolanimation");
            		domClass.add(this.domNode, "hiddentoolanimation");
            		//需要看一下如果baseMap没有隐藏的话,就将其隐藏掉
            		if(this.baseMap && domStyle.get(this.baseMap.domNode,"display")!="none"){
            			this.baseMap.onHide();
            		}
            	}else{
            		domClass.remove(this.domNode, "hiddentoolanimation");
            		domClass.add(this.domNode, "showtoolanimation");
            	}
            },
            /**点击basemap图标的时候触发*/
            _basemapBtnClick:function(){

        		if(this.baseMap == null){
        			this.baseMap = new BaseMap({alignNode:this.basemapBtn, close:true, hideWhenClickBody:true,"map":this.map});
        			this.baseMap.setTitle("BaseMap");
        		}
        		if(domStyle.get(this.baseMap.domNode,"display") == "none"){
        			this.baseMap.onShow();
        		}else{
        			this.baseMap.onHide();
        		}
            },
            /**帮助连接的页面*/
            helpBtnClick:function(){
            	window.open("../client/gistech/widgets/Help/help.html");
            },
            /**测距功能*/
            _meatureDistanceBtnClick:function(){
            	var _this=this;
            	this.map.setMapCursor("url(gistech/widgets/ManageTools/images/cursor_linemeasure.cur),auto");
            	MeasureLength.action(_this.map);
            },
            /**清除功能*/
            _clearBtn:function(){
//            	var layers=this.map.getLayersVisibleAtScale();
//            	for(var i=0;i<layers.length;i++){
//            		if(!(layers[i] instanceof esri.layers.TiledMapServiceLayer)){
//            			//this.map.removeLayer(layers[i]);
//            			layers[i].clear();
//            		}
//            	};
            	if(Global.widgets.right.hasExpand){
    				Global.widgets.right.expandLeft();
    			//	$('.dijitContentPane',Global.widgets.right.domNode).empty();  //这个是????
    			}
            	$(Global.widgets.BottomArrow2.toolbox.domNode).css('marginRight',parseInt($(this.domNode).css('marginRight',0)));
            	this.delPie();
            	ioc.map.cluster=false;//清除图层用
            	if(window.clusterResTree){
            		//window.clusterResTree.delPie();
            		if(Global.widgets.legendWidget){
            			Global.widgets.legendWidget.destroy();
            			Global.widgets.legendWidget=null;
            		}

            	}
            	Global.widgets.mapWidget.map.graphics.clear();
            	for(var l=0;l<ioc.map.graphiclayers.length;l++){
            		var glayer = ioc.map.graphiclayers[l];
            		if(glayer){
            			if(glayer.id=="CaiFuLayer"){
            				var maplayer = this.map.getLayer(glayer.id);
            				if(maplayer==null){
            					ioc.map.graphiclayers.splice(l,1);
            					continue;
            				}
            				maplayer.clear();
            				this.map.removeLayer(maplayer);
            				var valueHot= dijit.byId("valueHot");
            				if(valueHot){
            					valueHot.destroy();
            				}
            				ioc.map.graphiclayers.splice(l,1);
            			}else{
            				glayer.clear();
            			}
            		}
            	}
            },
            meatureAreaBtnClick:function(){
            	debugger;
            	var _this=this;
            	MeasureArea.action(_this.map);
            },
            
            _homeBtnClick:function(){
            	this.map.setExtent(new esri.geometry.Extent(ioc.map.initExt));
            },
            
            delPie:function(){
                for(var d=0;d<50;d++){ //TODO 这里删除一次不知为何节点有漏,50为多次删除
              	var canvasNodes = document.getElementsByTagName('canvas');
              	 for(var g=0;g<canvasNodes.length;g++){
              	  var aNode = document.getElementsByTagName('canvas')[g].parentNode;
              		 if(aNode != undefined){
              		  var bNode = aNode.parentNode
              		      if(bNode != undefined){
              			  if(bNode.attributes[0].nodeValue=="stat2"){
              			      //删除
              			       var pNode = document.getElementsByTagName('canvas')[g].parentNode.parentNode.parentNode;
              				for (var i = 0;i<pNode.childNodes.length;i++){
              				    pNode.removeChild(pNode.childNodes[i]);
              				}
              				break;
              			  }
              		      }
              		 }
              	 }}
              },
        })
    }
);